<?php
require_once ("../Login/config.php");
include_once ("../Login/databaseconnection.php");
$Subject=$_GET['s'];
$Exam_Name=$_GET['e'];
$Ana_In=$_GET['a'];
$Ana_In_Val=$_GET['av'];
if($Ana_In=="Zone")
{
$Poss_Sch_Ids="";
$get_schs=mysql_query("SELECT * FROM school WHERE Zone='".$Ana_In_Val."'");
if($get_schs)
{
while($rsch=mysql_fetch_array($get_schs))
{
if(empty($Pos_Sch_Ids))
{
$Pos_Sch_Ids=$rsch['School_Id'];
}
else
{
$Pos_Sch_Ids=$Pos_Sch_Ids.','.$rsch['School_Id'];
}

if(empty($Poss_Sch_Ids))
{
$Poss_Sch_Ids='"'.$rsch['School_Id'].'"';
}
else
{
$Poss_Sch_Ids=$Poss_Sch_Ids.',"'.$rsch['School_Id'].'"';
}
}
}
$To_Max_Min_ye=mysql_query("SELECT * FROM exam WHERE Subject='$Subject' AND Exam_Name='$Exam_Name' AND School_Id in(".$Poss_Sch_Ids.")");
while($rowMax_Min=mysql_fetch_array($To_Max_Min_ye))
{
if(empty($AllYear))
{
$AllYear=$rowMax_Min['Year'];
}
else
{
$AllYear=$AllYear.",".$rowMax_Min['Year'];
}
}
$values="";
$p_values="";
$S_values="";
$Y_values="";
$AllYears=explode(",",$AllYear);
$Max_year=$AllYears[0];
$Min_year=$AllYears[0];
for($y=1;$y<count($AllYears);$y++)
{
if($AllYears[$y]>=$Max_year)
{
$Max_year=$AllYears[$y];
}
if($AllYears[$y]<=$Min_year)
{
$Min_year=$AllYears[$y];
}
}
$no_Years=($Max_year-$Min_year)+1;
}
if($Ana_In=="District")
{
$Poss_Zones="";
$All_Poss_Schools='';
$get_schs=mysql_query("SELECT * FROM school WHERE District='".$Ana_In_Val."'");
if($get_schs)
{
while($rsch=mysql_fetch_array($get_schs))
{
if(empty($All_Poss_Schools))
{
$All_Poss_Schools='"'.$rsch['School_Id'].'"';
}
else
{
$All_Poss_Schools=$All_Poss_Schools.',"'.$rsch['School_Id'].'"';
}
}
$get_zones=mysql_query("SELECT DISTINCT(Zone) FROM school WHERE District='".$Ana_In_Val."'");
if($get_zones)
{
while($rzon=mysql_fetch_array($get_zones))
{
if(empty($Poss_Zones))
{
$Poss_Zones=$rzon['Zone'];
}
else
{
$Poss_Zones=$Poss_Zones.','.$rzon['Zone'];
}
}
}
if($Poss_Zones!="")
{
$Possible_Zones=explode(",",$Poss_Zones);
}
}
$To_Max_Min_ye=mysql_query("SELECT * FROM exam WHERE Subject='$Subject' AND Exam_Name='$Exam_Name' AND School_Id in(".$All_Poss_Schools.")");
while($rowMax_Min=mysql_fetch_array($To_Max_Min_ye))
{
if(empty($AllYear))
{
$AllYear=$rowMax_Min['Year'];
}
else
{
$AllYear=$AllYear.",".$rowMax_Min['Year'];
}
}
$values="";
$p_values="";
$Z_values="";
$Y_values="";
$AllYears=explode(",",$AllYear);
$Max_year=$AllYears[0];
$Min_year=$AllYears[0];
for($y=1;$y<count($AllYears);$y++)
{
if($AllYears[$y]>=$Max_year)
{
$Max_year=$AllYears[$y];
}
if($AllYears[$y]<=$Min_year)
{
$Min_year=$AllYears[$y];
}
}
$no_Years=($Max_year-$Min_year)+1;
}
if($Ana_In=="District")
{
$FZ_values="";
$FY_values="";
for($k=0;$k<$no_Years;$k++)
{
$year=$Min_year+$k;
if(empty($Y_values))
{
$Y_values=$year;
}
else
{
$Y_values=$Y_values.",".$year;
}
for($i=0;$i<count($Possible_Zones);$i++)
{
$Zone=$Possible_Zones[$i];
if(empty($Z_values))
{
$Z_values=$Zone;
}
else
{
	$Z_values=$Z_values.",".$Zone;
}
	$Poss_Sch_Ids="";
$get_schs=mysql_query("SELECT * FROM school WHERE Zone='".$Zone."'");
if($get_schs)
{
while($rsch=mysql_fetch_array($get_schs))
{
if(empty($Poss_Sch_Ids))
{
$Poss_Sch_Ids='"'.$rsch['School_Id'].'"';
}
else
{
$Poss_Sch_Ids=$Poss_Sch_Ids.',"'.$rsch['School_Id'].'"';
}
}
}
if(empty($FY_values))
{
$FY_values=$year;
}
else
{
$FY_values=$FY_values.",".$year;
}
if(empty($FZ_values))
{
$FZ_values=$Zone;
}
else
{
$FZ_values=$FZ_values.",".$Zone;
}
	$No_Of_Sat_Students="";
	$No_Of_Passed_Students="";
$get_res_per=mysql_query("SELECT * FROM exam WHERE Subject='$Subject' AND Exam_Name='$Exam_Name' AND Year='$year' AND School_Id in(".$Poss_Sch_Ids.")");
if(mysql_num_rows($get_res_per)==0)
{
$Per_Val="_";
		if(empty($p_values))
		{
		$p_values=$Per_Val;
		}
		else
		{
		$p_values=$p_values.",".$Per_Val;
		}
}
else
{
while($row_get_res_per=mysql_fetch_array($get_res_per))
{
		$No_Of_Sat_Students=$No_Of_Sat_Students+$row_get_res_per['No_Of_Sat_Students']." ";
		$No_Of_Passed_Students=$No_Of_Passed_Students+$row_get_res_per['No_Of_Passed_Students']."<br/>";
}
		if(!isset($No_Of_Sat_Students))
		{
		$Per_Val="_";
		if(empty($p_values))
		{
		$p_values=$Per_Val;
		}
		else
		{
		$p_values=$p_values.",".$Per_Val;
		}
		}
		if($No_Of_Sat_Students!=0)
		{
		$Per_Val=($No_Of_Passed_Students/$No_Of_Sat_Students)*100;
		if(empty($p_values))
		{
		$p_values=$Per_Val;
		}
		else
		{
		$p_values=$p_values.",".$Per_Val;
		}
		}
}
}
}
$P= str_replace("_","0",$p_values);
	$allpvalues= explode(",",$P);
	$max_value=$allpvalues[0];
	for($l=0;$l<count($allpvalues);$l++) 
	{
	if($max_value<=$allpvalues[$l+1])
	{
	$max_value=$allpvalues[$l+1];
	}
	}
	$Zo_values= explode(",",$Poss_Zones);
		$n=count($Zo_values);
		$Ye_values= explode(",",$Y_values);
		$ys=count($Ye_values);
		$bar_width=30;
	$total_bars=$no_Years;
		$len=$total_bars * $bar_width*$n;
	
    $img_width=$len+100;
	$img_height=600; 
	$margins=20;
	
	$graph_width=$img_width - $margins * 2;
	$graph_height=$img_height - $margins * 2; 
	$img=imagecreate($img_width,$img_height);

	$gap= 20;

	$background_color=imagecolorallocate($img,240,240,255);
	$border_color=imagecolorallocate($img,200,200,200);
	$line_color=imagecolorallocate($img,220,220,220);
 
	imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
	imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);
	
	$ratio= $graph_height/$max_value;
 	$horizontal_lines=30;
	$horizontal_gap=$graph_height/$horizontal_lines;
$b_color=imagecolorallocate($img,0,64,128);
	for($i=1;$i<=$horizontal_lines;$i++){
		$y=$img_height - $margins - $horizontal_gap * $i ;
		imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
		$v=intval($horizontal_gap * $i /$ratio);
		imagestring($img,0,5,$y-5,$v,$b_color);

	}
	
	for($i=0;$i< $total_bars; $i++)
	{
$yea=$Min_year+$i;
	for($j=0;$j<$n;$j++)
		{
		$Per_Val=$allpvalues[2*$i+$j];
		if($Per_Val=="0"||$Per_Val=="")
		{
			$Perc_Val="0%";
		}
		else
		{
		$Perc_Val=substr("$Per_Val",0,5)."%";
		}
		
		$Zo_va=substr("$Zo_values[$j]",0,4);
		$bar_color=imagecolorallocate($img,60*($j+1),24*($j+1),70*($j+1));
		$x1= $margins +($i+1)*$gap +$bar_width*($j)+$i*($bar_width*$n);
		$x2= $x1 + $bar_width;
  		$y1=$margins +$graph_height- intval($Perc_Val* $ratio) ;
		$y2=$img_height-$margins;	
		imagestring($img,0,$x1+3,$y1-10,$Perc_Val,$bar_color);
		imagestring($img,0,$x1+3,$img_height-18,$Zo_va,$bar_color); 		
		imagestring($img,0,$x1+3,$img_height-10,$yea,$bar_color);		
		imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
		}
	}
}
if($Ana_In=="Zone")
{
$FS_values="";
$FY_values="";
$Possible_Sch_Ids=explode(",",$Pos_Sch_Ids);
for($k=0;$k<$no_Years;$k++)
{
$year=$Min_year+$k;
if(empty($Y_values))
{
$Y_values=$year;
}
else
{
$Y_values=$Y_values.",".$year;
	}
for($i=0;$i<count($Possible_Sch_Ids);$i++)
{
$Sch_Id=$Possible_Sch_Ids[$i];
if(empty($S_values))
{
$S_values=$Sch_Id;
}
else
{
$S_values=$S_values.",".$Sch_Id;
}

$get_res_per=mysql_query("SELECT sum(No_Of_Sat_Students) as No_Of_Sat_Studentss,sum(No_Of_Passed_Students) as No_Of_Passed_Studentss FROM exam WHERE Subject='$Subject' AND Exam_Name='$Exam_Name' AND Year='$year' AND School_Id='$Sch_Id'");

	if($row_get_res_per=mysql_fetch_array($get_res_per))
{
$No_Of_Sat_Students=$row_get_res_per['No_Of_Sat_Studentss'];
		$No_Of_Passed_Students=$row_get_res_per['No_Of_Passed_Studentss'];
		if(!isset($No_Of_Sat_Students))
		{
		$Per_Val="_";
		if(empty($p_values))
		{
		$p_values=$Per_Val;
		}
		else
		{
		$p_values=$p_values.",".$Per_Val;
		}
		}
		if($No_Of_Sat_Students!=0)
		{
		$Per_Val=($No_Of_Passed_Students/$No_Of_Sat_Students)*100;
		if(empty($p_values))
		{
		$p_values=$Per_Val;
		}
		else
		{
		$p_values=$p_values.",".$Per_Val;
		}
		}
	}
}
}

$P= str_replace("_","0",$p_values);

	$allpvalues= explode(",",$P);
	$max_value=$allpvalues[0];
	for($l=0;$l<count($allpvalues);$l++) 
	{
	if($max_value<=$allpvalues[$l+1])
	{
	$max_value=$allpvalues[$l+1];
	}
	}
	$Sc_values= explode(",",$Pos_Sch_Ids);
		$n=count($Sc_values);
		$Ye_values= explode(",",$Y_values);
		$ys=count($Ye_values);
		$bar_width=30;
	$total_bars=$no_Years;
		$len=$total_bars * $bar_width*$n;
	
    $img_width=$len+100;
	$img_height=600; 
	$margins=20;
	
	$graph_width=$img_width - $margins * 2;
	$graph_height=$img_height - $margins * 2; 
	$img=imagecreate($img_width,$img_height);

	$gap= 20;

	$background_color=imagecolorallocate($img,240,240,255);
	$border_color=imagecolorallocate($img,200,200,200);
	$line_color=imagecolorallocate($img,220,220,220);
 
	imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
	imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);
	
	$ratio= $graph_height/$max_value;

 	$horizontal_lines=30;
	$horizontal_gap=$graph_height/$horizontal_lines;
$b_color=imagecolorallocate($img,0,64,128);
	for($i=1;$i<=$horizontal_lines;$i++){
		$y=$img_height - $margins - $horizontal_gap * $i ;
		imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
		$v=intval($horizontal_gap * $i /$ratio);
		imagestring($img,0,5,$y-5,$v,$b_color);
	}
	
	for($i=0;$i< $total_bars; $i++)
	{
$yea=$Min_year+$i;
	for($j=0;$j<$n;$j++)
		{
		$Per_Val=$allpvalues[2*$i+$j];
		if($Per_Val=="0"||$Per_Val=="")
		{
			$Perc_Val="0%";
		}
		else
		{
		$Perc_Val=substr("$Per_Val",0,5)."%";
		}
		$Sc_va=$Sc_values[$j];
		$bar_color=imagecolorallocate($img,50*($j+1),60*($j+1),25*($j+1));
		$x1= $margins +($i+1)*$gap +$bar_width*($j)+$i*($bar_width*$n);
		$x2= $x1 + $bar_width;
  		$y1=$margins +$graph_height- intval($Perc_Val* $ratio) ;
		$y2=$img_height-$margins;		
		imagestring($img,0,$x1+3,$y1-10,$Perc_Val,$bar_color);
		imagestring($img,0,$x1+3,$img_height-18,$Sc_va,$bar_color); 		
		imagestring($img,0,$x1+3,$img_height-10,$yea,$bar_color);		
		imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
		}
	}
	}
	header("Content-type:image/png");
	imagepng($img);
	 echo '<br/>';
  echo '<img src="../images/b_print.png" onClick="window.print()" align="right"/>';
?>
